package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.annotation.AutoFill;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.enumeration.OperationType;
import com.sky.vo.DishVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface DishMapper {

    /**
     * 根据分类id查询菜品数量
     * @param categoryId
     * @return
     */
    @Select("select count(id) from dish where category_id = #{categoryId}")
    Integer countByCategoryId(Long categoryId);

    /**
     * 通过菜品名字获取菜品数据
     * @param name
     * @return
     */

    @Select("select * from dish where name = #{name}")
    Dish getByDishName(String name);


    /**
     * 添加菜品数据
     *
     * @param dish
     * @return
     */

    @Options(useGeneratedKeys = true,keyProperty = "id")
    @Insert("insert into dish(name, category_id, price, image, description, status, create_time, update_time, create_user, update_user) " +
            "values(#{name},#{categoryId},#{price},#{image},#{description},#{status},#{createTime},#{updateTime},#{createUser},#{updateUser})")
    @AutoFill(OperationType.INSERT)
    int insert(Dish dish);


    Page<DishVO> pageQuery(DishPageQueryDTO dto);


    @AutoFill(OperationType.UPDATE)
    void update(Dish dish);

    List<Dish> queryByIds(List<Long> ids);

    void deleteBatch(List<Long> ids);

    @Select("select *from dish where id =#{id}")
    Dish queryById(Long id);

    @Select("select *from dish where category_id = #{categoryId}")
    List<Dish> queryByCategoryId(Long categoryId);


    @Select("SELECT * from dish where category_id = #{categoryId} and status = 1")
    List<Dish> selectActiveByCategoryId(Long categoryId);

    /**
     * 通过菜品id查询菜品
     * @param dishId
     * @return
     */
    @Select("SELECT * FROM dish where id = #{dishId}")
    Dish selectById(Long dishId);

    /**
     * 根据菜品 起售状态 统计菜品数量
     * @param enable
     * @return
     */
    @Select("SELECT count(1) FROM dish where status = #{status}")
    Integer countByStatus(Integer status);
}
